﻿using System;
using UniMana.Data;
using UniMana.Utils.Data;
using System.Linq;
using System.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors;
using System.Data;

namespace UniMana.Win.Training
{
    public partial class FrmStudent_Class_Add : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        private DataTable m_tblClass;
        private DataTable m_tblTable;

        private int m_iDepartmentKey = 0;
        private int m_iMajorKey = 0;
        private int m_iLevelKey = 0;
        private int m_iCourseKey = 0;
        private int m_iTrainingTypeKey = 0;        

        private int _Flag = 1;

        /// <summary>
        /// Khởi tạo
        /// </summary>
        /// <param name="m_iDepartmentKey"></param>
        /// <param name="m_iMajorKey"></param>
        /// <param name="m_iTrainingTypeKey"></param>
        /// <param name="m_iCourseKey"></param>
        /// <param name="m_iLevelKey"></param>

        public FrmStudent_Class_Add(int m_iDepartmentKey, int m_iMajorKey, int m_iLevelKey, int m_iCourseKey, int m_iTrainingTypeKey)
        {
            InitializeComponent();
            this.m_iDepartmentKey = m_iDepartmentKey;
            this.m_iMajorKey = m_iMajorKey;
            this.m_iLevelKey = m_iLevelKey;
            this.m_iCourseKey = m_iCourseKey;
            this.m_iTrainingTypeKey = m_iTrainingTypeKey;
        }

        /// <summary>
        /// Load dữ liệu lên form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void FrmStudent_Class_Add_Load(object sender, EventArgs e)
        {
            try
            {
                m_tblTable = new DataTable();
                m_tblTable = m_data.m_DCReadOnly.Personnel_Personnels.GetPersonnelKeyIDLastNameName().ToDataTable();
                if (m_tblTable.Rows.Count != 0)
                    gluPersonnel_Personnel.GetData(m_tblTable, "Personnel_PersonnelKey", "FullName");

                LoadClass();
                lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments.Where(tb => tb.StudentManage == true));
                lueCatalogue_Level.GetData(m_data.m_DCReadOnly.Catalogue_Levels);
                gluCatalogue_Major.GetData(m_data.m_DCReadOnly.Catalogue_Majors.GetMajorLevel(m_data.m_DCReadOnly.Catalogue_Levels, m_iDepartmentKey), "Catalogue_MajorKey");
                lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable());
                lueCatalogue_Course.GetData(m_data.m_DCReadOnly.Catalogue_Courses);

                if (m_iDepartmentKey > 0)
                    lueCatalogue_Department.EditValue = m_iDepartmentKey;
                if (m_iLevelKey > 0)
                    lueCatalogue_Level.EditValue = m_iLevelKey;
                if (m_iMajorKey > 0)
                    gluCatalogue_Major.EditValue = m_iMajorKey;
                if (m_iTrainingTypeKey > 0)
                    lueCatalogue_TrainingType.EditValue = m_iTrainingTypeKey;
                if (m_iCourseKey > 0)
                    lueCatalogue_Course.EditValue = m_iCourseKey;
            }
            catch { }
        }

        /// <summary>
        /// Lấy thông tin Khoa, ngành, bậc đào tạo, khóa, loại hình đào tạo
        /// </summary>

        private void GetData()
        {
            try { m_iDepartmentKey = int.Parse(lueCatalogue_Department.EditValue.ToString()); }
            catch { m_iDepartmentKey = 0; }
            try { m_iMajorKey = int.Parse(gluCatalogue_Major.EditValue.ToString()); }
            catch { m_iMajorKey = 0; }
            try { m_iLevelKey = int.Parse(lueCatalogue_Level.EditValue.ToString()); }
            catch { m_iLevelKey = 0; }
            try { m_iCourseKey = int.Parse(lueCatalogue_Course.EditValue.ToString()); }
            catch { m_iCourseKey = 0; }
            try { m_iTrainingTypeKey = int.Parse(lueCatalogue_TrainingType.EditValue.ToString()); }
            catch { m_iTrainingTypeKey = 0; }
        }

        /// <summary>
        /// Lấy danh sách lớp
        /// </summary>

        private void LoadClass()
        {
            m_tblClass = new DataTable();
            m_tblClass = m_data.m_DCReadOnly.Training_Classes.GetListClassWithID("", m_iDepartmentKey, 0, 0, 0, 0).ToDataTable();
            if (m_tblClass.Rows.Count != 0)
            {
                gctClass.DataSource = m_tblClass;
            }
            else
            {
                gctClass.DataSource = null;
            }
        }

        /// <summary>
        /// Kiểm tra thêm lớp
        /// </summary>
        /// <returns></returns>

        private bool CheckToAdd(string _ID)
        {
            if (m_data.m_DCReadOnly.Training_Classes.CheckIDClassIdentical(_ID))
                return false;
            else
                return true;
        }

        /// <summary>
        /// Kiểm tra đủ thông tin Khoa, ngành, bậc đào tạo, khóa, loại hình đào tạo
        /// </summary>
        /// <returns></returns>

        private bool CheckInfo()
        {
            if ((m_iDepartmentKey <= 0) || (m_iMajorKey <= 0) || (m_iLevelKey <= 0) || (m_iCourseKey <= 0) || (m_iTrainingTypeKey <= 0))
                return false;
            else
                return true;
        }

        /// <summary>
        /// Thêm lớp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                GetData();
                if ((txtID.Text != "") && (txtName.Text != ""))
                {
                    if (CheckToAdd(txtID.Text))
                    {
                        if (CheckInfo())
                        {
                            Training_ClassInfo cls = new Training_ClassInfo();
                            cls.Properties.ID = txtID.Text;
                            cls.Properties.Name = txtName.Text;
                            cls.Properties.Catalogue_DepartmentKey = m_iDepartmentKey;
                            cls.Properties.Catalogue_MajorKey = m_iMajorKey;
                            cls.Properties.Catalogue_TrainingType = m_iTrainingTypeKey;
                            cls.Properties.Catalogue_CourseKey = m_iCourseKey;
                            cls.Properties.Catalogue_LevelKey = m_iLevelKey;
                            cls.Properties.Training_TrainingProgramKey = m_data.m_DCReadOnly.Training_TrainingPrograms.ReturnTrainingProgramWithTrainingTypeMajorLevelCourse(m_iTrainingTypeKey,
                                m_iMajorKey,
                                m_iLevelKey,
                                m_iCourseKey);
                            if (rad2.Checked)
                                cls.Properties.Yearly = false;
                            else
                                cls.Properties.Yearly = true;
                            try
                            {
                                cls.Properties.Personnel_PersonnelKey = Guid.Parse(gluPersonnel_Personnel.EditValue.ToString());
                            }
                            catch { }
                            cls.Save();
                            XtraMessageBox.Show("Đã thêm", "Thông báo");
                            LoadClass();
                        }
                        else
                            XtraMessageBox.Show("Thông tin Khoa, bậc đào tạo , chuyên ngành, loại hình đào tạo, khóa học chưa đầy đủ", "Thông báo");
                    }
                    else
                        XtraMessageBox.Show("Mã lớp đã tồn tại", "Thông báo");
                }
                else
                    XtraMessageBox.Show("Chưa nhập đủ thông tin Lớp", "Thông báo");
            }
            catch { }
        }

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (_Flag == 1)
                {
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) <= 0)
                    {
                        // Nạp lại dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học

                        lueCatalogue_Level.EditValue = null;
                        lueCatalogue_Level.GetData((this.m_data.m_DCReadOnly.Catalogue_Levels.GetLevelInTrainingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);

                        gluCatalogue_Major.EditValue = null;
                        gluCatalogue_Major.GetData((this.m_data.m_DCReadOnly.Catalogue_Majors.GetMajorInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, m_data.m_DCReadOnly.Catalogue_Levels)).ToDataTable(), "Catalogue_MajorKey", noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        lueCatalogue_Course.GetData((this.m_data.m_DCReadOnly.Catalogue_Courses.GetCourseKeyNameInTrainingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);
                    }
                    else
                    {
                        // Lấy dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học theo khoa

                        lueCatalogue_Level.EditValue = null;
                        this.lueCatalogue_Level.GetData((this.m_data.m_DCReadOnly.Catalogue_Levels.GetLevelByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);

                        gluCatalogue_Major.EditValue = null;
                        this.gluCatalogue_Major.GetData((m_data.m_DCReadOnly.Catalogue_Majors.GetMajorByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, m_data.m_DCReadOnly.Catalogue_Levels, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), "Catalogue_MajorKey", noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        this.lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        this.lueCatalogue_Course.GetData((this.m_data.m_DCReadOnly.Catalogue_Courses.GetCourseByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);
                    }
                }
            }
            catch { }
        }

        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue) > 0)
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) > 0)
                        gluCatalogue_Major.EditValue = (m_data.m_DCReadOnly.Catalogue_Majors.GetMajorKeyByDepartmentLevel(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue), GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue))).ToDataTable().Rows[0]["Catalogue_MajorKey"];
            }
            catch { }
        }

        private void gluCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue) > 0)
                {
                    _Flag = 0;
                    lueCatalogue_Department.EditValue = (m_data.m_DCReadOnly.Catalogue_Majors.GetDepartmentKeyByMajor(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue))).ToDataTable().Rows[0]["Catalogue_DepartmentKey"];
                    _Flag = 1;

                    lueCatalogue_Level.EditValue = (m_data.m_DCReadOnly.Catalogue_Majors.GetLevelKeyByDepartmentMajor(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue), GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue))).ToDataTable().Rows[0]["Catalogue_LevelKey"];
                }
            }
            catch { }
        }
    }
}
